import 'package:dio/dio.dart';

/// 通用日志拦截器
class CommonLogInterceptor extends InterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    print('HTTP请求开始: -->${options.method} ${options.baseUrl}${options.path}');
    print('request heads: ${options.headers.toString()}');
    if (options.method == 'GET') {
      print('request params: ${options.queryParameters.toString()}');
    } else {
      print('request params: ${options.data.toString()}');
    }
    // 继续请求责任链
    super.onRequest(options, handler);
  }

  @override
  void onResponse(
    Response<dynamic> response,
    ResponseInterceptorHandler handler,
  ) {
    if (response.data != null) {
      print('response : ${response.data.toString()}');
    }
    super.onResponse(response, handler);
  }

  @override
  void onError(
    DioException err,
    ErrorInterceptorHandler handler,
  ) {
    print(
        'onError errorCode: ${err.response?.statusCode}, errorMessage: ${err.message}');
    super.onError(err, handler);
  }
}
